﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Img;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.Img
{
    public class CreateImgHandler : IRequestHandler<CreateImgCommand, APIResult<int>>
    {
        private readonly IBaseRepository<ImgInformationModel> imgRepo;
        private readonly IMapper mapper;

        public CreateImgHandler(IBaseRepository<ImgInformationModel> imgRepo,
                                IMapper mapper)
        {
            this.imgRepo = imgRepo;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(CreateImgCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<ImgInformationModel>(request);
            int r = imgRepo.AddAsync(model).Result;
            if (r > 0)
            {
                result.Message = "添加照片成功";
                result.Data = r;
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "添加照片失败";
                result.Data = r;
                result.Code = APIEnum.Fail;
            }
            return Task.FromResult(result);
        }
    }
}
